package ru.amse.tsyganov.jumleditor.main;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import ru.amse.tsyganov.jumleditor.commands.ViewsManager;
import ru.amse.tsyganov.jumleditor.graphicaleditor.GraphicalEditor;
import ru.amse.tsyganov.jumleditor.graphicaleditor.ItemsSelectionListener;
import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.properties.PropertiesTable;
import ru.amse.tsyganov.jumleditor.view.ActivityGraphView;
import ru.amse.tsyganov.jumleditor.view.SelectedItems;

public class Editor {
	
	private static final String SHELL_CAPTION = "Editor";
		
	private static final int INITIAL_SHELL_WIDTH = 500;
	
	private static final int INITIAL_SHELL_HEIGHT = 500;
	
	private static final int PROPERTIES_TABLE_HEIGHT = 150;
		
	private final Display display;
	
	private final Shell sShell;
	
	private final GraphicalEditor graphicalEditor;
	
	private final ViewsManager viewsManager;
	
	private final TabFolder tabFolder; 
	
	private final PropertiesTable propertiesTable;
	
	private final ItemsSelectionListener itemsSelectionListener = 
		new ItemsSelectionListener() {

			public void selectedItemsChanged(SelectedItems selectedItems) {
				if (selectedItems.selectedStatesSize() + 
						selectedItems.selectedTransitionsSize() == 1) {
					
					if (selectedItems.selectedStatesSize() == 1) {
						propertiesTable.setSource(
							selectedItems.getSelectedStates().
								iterator().next());
					}
					
					if (selectedItems.selectedTransitionsSize() == 1) {
						propertiesTable.setSource(
							selectedItems.getSelectedTransitions().
								iterator().next());
					}
				} else {
					propertiesTable.clear();
				}
			}
		
		};
	
	public Editor(ActivityGraphView<ActivityGraph> activityGraphView) {
		
		display = new Display();
		
		sShell = new Shell(display);
		
		final int VIEWS_MANAGER_SIZE = 20; 
		viewsManager = new ViewsManager(VIEWS_MANAGER_SIZE);
		
		graphicalEditor = new GraphicalEditor(sShell, activityGraphView, viewsManager);
		
		tabFolder = new TabFolder(sShell, SWT.NONE);
		
		propertiesTable = new PropertiesTable(tabFolder, 
				INITIAL_SHELL_WIDTH - 20, viewsManager);
		
		createSShell();
		
		graphicalEditor.addSelectionListener(itemsSelectionListener);
		
		openAndWaitEvents();
	}

	private void openAndWaitEvents() {
		sShell.open();
		
		while(!sShell.isDisposed()) {
		   if(!display.readAndDispatch()) {
		       display.sleep();
		   }
		}
		display.dispose();
	}

	private void createSShell() {
		
		sShell.setText(SHELL_CAPTION);
		sShell.setImage(new Image(null, "icons/activityDiagram_on.gif"));
		
		initGraphicalEditor();
		initPropertiesTable();
		
		FormLayout shellFormLayout = new FormLayout();
		shellFormLayout.marginHeight = 0;
		shellFormLayout.marginWidth = 0;
		shellFormLayout.marginBottom = 0;
		shellFormLayout.marginLeft = 0;
		shellFormLayout.marginRight = 0;
		shellFormLayout.marginTop = 0;
		sShell.setLayout(shellFormLayout);
				
		sShell.setSize(new Point(INITIAL_SHELL_WIDTH, 
				INITIAL_SHELL_HEIGHT));
		
		assert sShell != null;
	}

	private void initGraphicalEditor() {
		assert sShell != null;
		
		FormData graphicalEditorFormData = new FormData();
		graphicalEditorFormData.left = new FormAttachment(0, 0);
		graphicalEditorFormData.right = new FormAttachment(100, 0);
		graphicalEditorFormData.bottom = new FormAttachment(100, -PROPERTIES_TABLE_HEIGHT);
		graphicalEditorFormData.top = new FormAttachment(0, 0);
		graphicalEditor.setLayoutData(graphicalEditorFormData);
	
	}
	
	private void initPropertiesTable() {
		assert sShell != null;
		assert propertiesTable != null;

		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setControl(propertiesTable.getTable());
		tabItem.setText("Properties");
		
		FormData tabFolderFormData = new FormData();
		tabFolderFormData.left = new FormAttachment(0, 0);
		tabFolderFormData.right = new FormAttachment(100, 0);
		tabFolderFormData.bottom = new FormAttachment(100, 0);
		tabFolderFormData.top = new FormAttachment(graphicalEditor, 0);
		tabFolder.setLayoutData(tabFolderFormData);	
	}
	
}
